<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0px;
            margin: 0px;
        }

        html,
        body {
            height: 100%;
            background-color: #000;
        }

        .xueT {
            width: 200px;
            height: 20px;
            border: solid #fff 1px;
        }

        .xueT span {
            width: 200px;
            height: 20px;
            display: inline-block;
            background-color: red;
        }
    </style>
</head>

<body>
    <div class="xueT">
        <span style="width: 200px;"></span>
    </div>

    <script>

      var time = setInterval(function(){
      var span = document.querySelector(".xueT span");
            var width = parseInt(span.style.width);
            width -= 8;
            span.style.width = width + "px";
            if (width <= 0) {
                alert("你输啦，下次继续努力吧");
                clearInterval(time);
            }
        },1000)


        //定时器每一秒获取随机小星星一个
        function random(min, max) {
            return Math.floor(Math.random() * (max - min + 1) + min);
        }
        setInterval(function () {
            var img = document.createElement("img");
            img.src = "./star.gif";
            img.width = random(40, 60);
            img.height = random(40, 60);
            img.style.position = "absolute";
            var maxWidth = (window.innerWidth || document.documentElement.clientWidth) - img.width;
            var maxHeight = (window.innerHeight || document.documentElement.clientHeight) - img.height;
            img.style.left = random(0, maxWidth) + "px";
            img.style.top = random(0, maxHeight) + "px";
            document.body.appendChild(img);
            
        }, 1000)
     
        //点击小星星可进行删除
        document.body.onclick = function (e) {
            var e = e || window.event;
            var target = e.target || e.srcElement;
            if (target.nodeName == "IMG")
            document.body.removeChild(target);
            var span = document.querySelector(".xueT span");
            var width = parseInt(span.style.width);
                span.style.width=(width+2)+"px";
        }


        //注：血条溢出的情况不知道怎么处理 
        //    漏洞bug太多 需改进 认真研究
    </script>
</body>

</html>